package cn.ciis.sys.gencode;

import cn.ciis.sys.gencode.util.JfGenerator;
import cn.ciis.sys.taxonomy.TaxonomyAdminService;
import cn.ciis.basic.base.BaseController;
import cn.ciis.basic.config.CiisConfig;
import cn.ciis.basic.kit.CiisKit;
import cn.ciis.basic.model.GeneratorModel;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Prop;
import com.jfinal.kit.StrKit;

import java.io.File;

/**
 * 代码生成
 *
 * @author 冉志林
 */
public class GenAdminController extends BaseController {

  @Inject
  GenAdminService srv;
  @Inject
  TaxonomyAdminService taxonomyAdminService;

  public void index() {

    set("taxList",taxonomyAdminService.findAll());
    render("index.html");
  }

  public void opt(String ide){
    Prop p= CiisConfig.p;
    if (StrKit.isBlank(ide)) {
      kFail("没有获取到对应的系统信息");
      return;
    }
    kSuccess(srv.tableList(p.get("mysql.databaseName"),ide));
  }

  public void genModel() {
    GeneratorModel generatorModel = new GeneratorModel();
    generatorModel.gen();
    kSuccess();
  }

  public void genCode() {
    String tableInfo = get("tableName");
    String tableName=tableInfo.split("-")[0];
    String tableNameSort=tableName.substring(tableName.indexOf("_")+1);
    String tableNote=tableInfo.split("-")[1];
    String srcFolder = get("srcFolder");
    String sqlPage = get("sqlPage");

    String className = CiisKit.upperFirstLatter(CiisKit.underlineToCamel(tableNameSort));

    @SuppressWarnings("unused")
    JfGenerator jfGenerator = JfGenerator.me
            .setSrcFolder("src/main/java/cn/ciis"+File.separator)
            .setViewFolder("src/main/webapp"+ File.separator+"_"+srcFolder)
            .setPackageBase(srcFolder)
            .setBasePath("")
            .javaRender(className,tableName,tableNote,srcFolder)
            .htmlRender(className,tableName,tableNote,srcFolder)
            .sqlRender(tableName,tableNote,sqlPage)
            .routesRender(className,tableNote,srcFolder);
    kSuccess();
  }

}
